/*
 * Copyright© 2013-2018 YZTC 
 * Author zhenghl 
 * 本公司保留所有下述内容的权利; 
 * 本内容为保密信息，仅限本公司内部使用; 
 * 非经本公司书面许可，任何人不得外泄或用于其他目的; 
*/
package com.wxmp.core.util;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
@Component
public class SpringContextFactory implements ApplicationContextAware{
	 private static ApplicationContext ctx ;
	 public static ApplicationContext getSpringContext(){
		 return ctx;
	 }
	 public static Object getBean(String beanKey){
		 return ctx.getBean(beanKey);
	 }
	 public static ApplicationContext getWebSpringContext(HttpServletRequest request){
		 return getWebSpringContext(request.getSession());
	 }
	 public static ApplicationContext getWebSpringContext(HttpSession session){
		 return getWebSpringContext(session.getServletContext());
	 }
	 public static ApplicationContext getWebSpringContext(ServletContext servletContext){
		 return WebApplicationContextUtils.getWebApplicationContext(servletContext);
	 }
	 public static Object getWebBean(HttpServletRequest request,String beanKey){
		 return getWebSpringContext(request).getBean(beanKey);
	 }
	 public static Object getWebBean(HttpSession session,String beanKey){
		 return getWebSpringContext(session).getBean(beanKey);
	 }
	 public static Object getWebBean(ServletContext servletContext,String beanKey){
		 return getWebSpringContext(servletContext).getBean(beanKey);
	 }
	@Override
	public void setApplicationContext(ApplicationContext arg0)throws BeansException {
		ctx = arg0;
	}
}
